<!DOCTYPE html>
<html>
 <head id="head">
  <meta charset="UTF-8">
  <title></title>
  <script src="/static/market/axios.min.js"></script>
  <script src="/static/market/jquery.min.js"></script>

  <script>
	function replaceSource(content, host)
	{
		let linkReg=/<link(?:.*?)href=[\"\'](\/.+?)[\"\'](?!<)(?:.*)\>(?:[\n\r\s]*?)(?:<\/link>)*/gmi;
		let scriptReg=/<script(?:.*?)src=[\"\'](\/.+?)[\"\'](?!<)(?:.*)\>(?:[\n\r\s]*?)(?:<\/script>)*/gmi;
		let imgReg=/<img(?:.*?[\n\r\s]*.*?)src=[\'\"](\/.+?)[\'\"](?!<)(?:.*?[\n\r\s]*.*?)\/*>/gmi;

		let str = content;
		while(true)
		{
				let newStr = str.replace(linkReg, function (capture, src) {
					return capture.replace(src, host + src);
				});
				
				newStr = newStr.replace(scriptReg, function (capture, src) {
					return capture.replace(src, host + src);
				});
				
				newStr = newStr.replace(imgReg, function (capture, src) {
						return capture.replace(src, host + src);
				});
				
				if(newStr == str){
					break;
				}
				else{
					str = newStr;
				}
		}
		
		return str;
	}

	function loadFrame() {

		axios.get("/web/market").then((rsp)=>{

			window.localStorage.marketUrl = rsp.data.url;

			axios.get(window.localStorage.marketUrl + '/load_embed.html?' + (new Date()).getTime())
			.then(function (response) {
				//获取远程html页面
				let content = response.data;

				let newStr = replaceSource(content, window.localStorage.marketUrl);

				$('#head').append(newStr);
				
				setTimeout(()=>{
					parent.document.loadFrameSucc();
				}, 2000);
			})
			.catch(function (error) {
				parent.document.loadFrameError();
			});
		})

	}
	loadFrame();

  </script>
</head>
<body>
	<div id="market"></div>
</body>
</html>